let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})
const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Dealer_Name: [{ required: true, message: '请输入经销商名称', trigger: 'blur' }],
        Tax_Number: [{ required: true, message: '请输入纳税识别号', trigger: 'blur' }],
        Dealer_Address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        // address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
      },
      brand: [],
      article: [],
      text: [],
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      areaList: [],
      isdisabled: true,
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      dialogImageUrl: '',
      dialogVisible: false,
      dealer_bank_pic: [],
      fileList: [],
      form: {
        Dealer_SerialNumber: "",//经销商流水号
        Dealer_Name: '',//经销商名字
        Tax_Number: '',//纳税识别号
        Dealer_Address: '',//经销商地址
        Account_Number: '', //预收款银行卡号
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          dealer_bank_account: [{
            Dealer_Bank_Account_SerialNumber: "",//经销商银行流水号
            Bank_Name: '',//开户银行
            Account_Number: '',//账号
          }],
          dealer_bank_pic: [{ //经销商材料上传
            Dealer_Bank_Pic_SerialNumber: "",//材料流水号
            Picture_Url: '',//图片地址
          }],
        }
      },
      loading: false,
      value: [],
    }
  },
  mounted() {
    this.form.Enterprise_Info_SerialNumber = $.cookie("enterprise");

  },
  methods: {
    submitForm(formName) {
      this.form.Dealer_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}/${this.address}`
      let _this = this
      console.log(formName)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          _this.form.Children.dealer_bank_pic[0].pic = ""
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                dealer_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './Dealer.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            },
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //供应商相关材料照片
    upload(file) {
      let _this = this
      new Compressor(file, {
        quality: 0.08,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            _this.form.Children.dealer_bank_pic[0].pic = e.target.result
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  _this.form.Children.dealer_bank_pic[0].Picture_Url = Result.Picture_Url
                } else {
                  _this.$message.error(Result.msg)
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //查看图片
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    //删除图片
    handleRemove() {
      this.form.Children.dealer_bank_pic[0].Picture_Url = ""
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})